boolean firstPress = false;
int x0 ;
int y0 ;
int x1 ;
int y1 ;

void setup() {
  size(640, 360);
  background(255);
  noFill();
}

void mousePressed() {
  if (firstPress) {
    x1 = mouseX;
    y1 = mouseY;
    firstPress = false;
    stroke(random(1, 250), random(1, 250), random(1, 250));
  }
  else {
    x0 = mouseX;
    y0 = mouseY;
    firstPress = true;
  }
}

void draw() {
  //if (firstPress && x1!= mouseX && y1 !=  mouseY) {
  if (firstPress) {
    background(255);
    x1 = mouseX;
    y1 = mouseY;
   // stroke(random(1, 250), random(1, 250), random(1, 250));

    int x, y;
    int a=abs(x1-x0),b=abs(y1-y0);
    // f(x,y) = b^2*x^2 + a^2*y^2 - a^2*b^2 = 0

    int d = 4*b*b - 4*a*a*b + a*a;
    x = x0;
    y = y0+b;
    
    while (a*a*(2*(y-y0)-1) > b*b*2*((x-x0)+1)) {
      ellipsePoint(x,y);
      x += 1;
      y += ((d>0)?-1:0);
      d += 4*b*b*(2*(x-x0) + 3) + ((d>0)?4*a*a*(-2*(y-y0)+2):0);
    }
    while ((y-y0) >= 0) {
      ellipsePoint(x,y);
      y += -1;
      x += ((d<0)?1:0);
      d += 4*a*a*(-2*(y-y0)+3) + ((d<0)?4*b*b*(2*(x-x0) + 3):0);
    }
  }
}

void ellipsePoint(int x,int y){
      point(x, y);
      point(x,y0-(y-y0));
      point(x0-(x-x0),y);
      point(x0-(x-x0),y0-(y-y0));
  
}


